package com.example.online_exam_system.controller;

import com.example.online_exam_system.commons.GridTable;
import com.example.online_exam_system.commons.Result;
import com.example.online_exam_system.logic.ITeacherLogicService;
import com.example.online_exam_system.service.StuSubService;
import com.example.online_exam_system.service.SubjectService;
import com.google.common.collect.Table;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author lianglong
 * @date 2022-03-27
 */
@Slf4j
@RestController
public class AdminControler {
    @Autowired
    ITeacherLogicService teacherLogicService;
    @Autowired
    StuSubService stuSubService;
    @Autowired
    SubjectService subjectService;

    @PostMapping("admin/html/device/adminteacher")
    public GridTable adminteacher(){
        GridTable gridTable = new GridTable();
        List<Map<String, Object>> allInfo = teacherLogicService.getAllInfo();
        gridTable.setTotal(String.valueOf(allInfo.size()));
        gridTable.setRows(allInfo);
        allInfo.forEach(info->{
            StringBuffer sb = new StringBuffer();
            List<Map<String, Object>> teachersubs = stuSubService.getTeacherSub(MapUtils.getString(info, "teacherid"));
            teachersubs.forEach(teacherid->{
                sb.append(MapUtils.getString(subjectService.getSubjectNameById(MapUtils.getString(teacherid,"subjectid")),"subjectname"));
                sb.append(",");
            });
            info.put("subjects",sb.toString());
        });
        log.debug(gridTable+"");
        return gridTable;
    }
    @PostMapping("modifyselected")
    public Result modifyselected(@RequestBody Map<String,Object> map){
        return null;
    }
}
